package com.gt.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author wangdong
 * @since 2022-08-09
 */
@Slf4j
@Component
public class WebServiceUtil {
    /**
     * 版本
     */
    private static String version;
    /**
     * 平台应用码
     */
    private static String appCode;
    /**
     * 平台密码
     */
    private static String password;

    public static void setVersion(String version) {
        WebServiceUtil.version = version;
    }

    public static void setAppCode(String appCode) {
        WebServiceUtil.appCode = appCode;
    }

    public static void setPassword(String password) {
        WebServiceUtil.password = password;
    }
    public static String createStr(String data, String messageCategory){
        /**
         * 创建平台规定字符串格式
         */
        String str = String.format("<ESBEnvelope\n" +
                "    xmlns=\"http://ESB.TopSchemaV2\">\n" +
                "    <ESBHeader>\n" +
                "        <HeaderControl AppCode=\"%s\" Password=\"%s\" MessageCategory=\"%s\" Version=\"%s\" CreateTime=\"%s\" />\n" +
                "    </ESBHeader>\n" +
                "    <ESBBody>\n" +
                "        <BodyControl CallType=\"\"/>\n" +
                "        <BusinessRequest>\n" +
                "            <![CDATA[%s]]>\n" +
                "        </BusinessRequest>\n" +
                "    </ESBBody>\n" +
                "</ESBEnvelope>", appCode, password,messageCategory,version,DateUtils.getDateTime(),data);
        return str;
    }

    /**
     * 发送数据
     *
     * @param str    发送的字符串
     * @param method 调用方法
     * @return 解密后的字符
     */

    public static String sendData(String str, String method)  {
        /***构造平台数据格式*/
        String data = createStr(str, method);
        log.info("调用平台数据：{}",data);

        /**加密数据**/
        String encryptData = HttpUtils.sendEncrypt(data);
        log.info("加密數據：{}",encryptData);

        /**发送数据**/
        long start = System.currentTimeMillis();
        String result = HttpUtils.sendData(encryptData);
        long time =  System.currentTimeMillis() - start;
        log.info("调用平台消耗时间【{}】",time);

        if (StringUtils.isBlank(result)){
            return null;
        }

        /**解密返回的数据**/
        String decryptData = HttpUtils.sendDecrypt(result);
        log.info("解密返回的数据=={}",decryptData);
        return decryptData;
    }

    public static String sendDataPost(String str, String method)  {
        /***构造平台数据格式*/
        String data = createStr(str, method);
        log.info("调用平台数据：{}",data);

        /**加密数据**/
        String encryptData = HttpUtils.sendEncrypt(data);
        log.info("加密數據：{}",encryptData);

        /**发送数据**/
        long start = System.currentTimeMillis();
        String result = HttpUtils.sendDataPost(encryptData);
        long time =  System.currentTimeMillis() - start;
        log.info("调用平台消耗时间【{}】",time);

        /**解密返回的数据**/
        String decryptData = HttpUtils.sendDecrypt(result);
        log.info("解密返回的数据=={}",decryptData);
        return decryptData;
    }
}
